package LawsonTransformJava.pgm;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;

import LawsonTransformJava.xref.CrossRef;
import LawsonTransformJava.xref.XrefPosition;

import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOReview;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblReview;

public class PA526 extends DAObject implements 
 DAOReview, Runnable {

 public PA526(File output, File errOutput, File errReport) {
		try {
			this.output = new FileWriter(this.outputFile = output);
			this.errOutput = new FileWriter(this.errOutputFile = errOutput);
			this.errReport = new Report(errReport, new Date());

			// initialization 
			init();
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
}

private void init() throws IOException {
	    // Set the data and xref variables
	    data = DAOFactoryOracleDB.getDAOOracleDB(true);
	    
		errReport.setPgmName(this.getClass().getSimpleName());

	    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
	    stmtData = data.getDataStatement();	    
	    countStmtData = data.getCountStatement();
}

public TblReview findReview() {
		try {
			TblReview rev = new TblReview();
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + 
					(whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next())
				hasMoreRecs = true;
			else
				hasMoreRecs = false;
			rev = (TblReview) createObject(rslt, rev);
			return rev;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

public TblReview findReview(String employeeNumber) {
	try {		
		TblReview rev = new TblReview();
		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " WHERE " + TABLE_NAME + ".EMPLOYEE='" + employeeNumber + "'";
		rslt = data.getDataStatement().executeQuery(sqlData);
		rsmd = rslt.getMetaData();
		rslt.next();
		//@ TODO Add the creation of an Employee object here
		rev = (TblReview) createObject(rslt, rev);
		return rev;
		//createFields(emp);
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	} catch (InvocationTargetException ite) {
		System.out.println("InvocationException: " + ite);
	} catch (IllegalAccessException iae) {
		System.out.println("IllegalAccessException: " + iae);
	}
	return new TblReview();
}

public TblReview nextReview() {
		try {
			TblReview rev = new TblReview();
			if (rslt.next()) {
				hasMoreRecs = true;
				rev = (TblReview) createObject(rslt, rev);
			}
			else
				hasMoreRecs = false;
			return rev;			
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

public int countReviews() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*)" +
				"FROM " + data.getSchema() + "." + TABLE_NAME + " " +
				(whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
}

public void setWhereClause(String where) {
	  this.whereClause = where;
}

public void run() {
	  TblReview rev = null;
	  
	  try {
		  //Update current date/time
		  System.out.println(errReport.getName() + " Start  " + Report.getDateFormat().format(new Date()));
		
		rev = findReview();
		while (hasMore()) {
			writePA526Layout(rev);
			rev = nextReview();
		}
		//Update current date/time
		System.out.println(errReport.getName() + " Finish " + Report.getDateFormat().format(new Date()));

		//Write Record Counts
		errReport.writeRecordCounts(this.countReviews());
		
		//Reclaim Resources
		fileCleanup();

	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	}

}

	//Output the HR513 format
	public void writePA526Layout(TblReview rev) throws IOException {

		String writeValue = null;

		writeValue =
				"1" + "," +
				rev.getEmployee()	  	+ "," +
				rev.getSched_date() 	+ "," +
				rev.getCode()			+ "," +
				rev.getActual_date()	+ "," +
				rev.getRating() 		+ "," +
				rev.getLast_name()		+ "," +
				rev.getFirst_name()		+ "," +
				rev.getMiddle_init()	+ "," +
				rev.getBy_employee()	+ "," +
				rev.getModel_name()		+ "," +
				rev.getTotal_score()	+ "," +
				rev.getDescription()	+ "," +
				rev.getRev_schedule()	+ "," +
				rev.getNext_review()	+ "," +
				rev.getNext_rev_code()	+ "," +
//				rev.getSched_Next_Rev() //Sched_next_rev
				""						//Sched_next_rev			
			;
		
		//Increment Record Written
		errReport.addWrittenCnt();
		//Write to Load File
		this.output.write(writeValue + "\n");
		this.output.flush();
	}
	
	public void fileCleanup() throws IOException {
		errReport.flush();
		errReport.close();
		output.flush();
		output.close();
		errOutput.flush();
		errOutput.close();
	}
}